package dbclass;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import classes.DbClass;

import beans.Voto;

public class VotoJDBC implements VotoDAO{	

	private Connection con;
	
	
	public Voto insert(Voto voto) throws SQLException, ClassNotFoundException
	{
		con = DbClass.getConnection();
		Statement stm = con.createStatement();
		
		boolean check =  stm.execute("INSERT INTO voto (id, ip, nota,id_noticia) VALUES (" +
				"NULL, '" + voto.getIp() + "', '" + voto.getNota() + "', '" + voto.getId_noticia()+ "')");
		

		
		if(check)
		{
			return voto;
		}	
		return null;	
	}
	
	public Voto update(Voto voto) throws SQLException, ClassNotFoundException
	{
		con = DbClass.getConnection();
		Statement stm = con.createStatement();
		
		boolean check = stm.execute("UPDATE voto SET id = '" + voto.getId()+ "', ip = '" + voto.getIp() +
				"', nota = '" + voto.getNota() + "', id_noticia = '" + voto.getId_noticia() + "' WHERE id = '" + voto.getId() + "'");
		
		if(check)
		{
			return voto;
		}	
		return null;
	}

	public boolean delete(long id) throws ClassNotFoundException, SQLException {
		con = DbClass.getConnection();
		Statement stm = con.createStatement();
		int check = stm.executeUpdate("DELETE FROM voto WHERE id = '" + id + "' LIMIT 1");
		if(check > 0)
			return true;
		return false;
	}

	public List<Voto> findAll() throws SQLException, ClassNotFoundException {
		List<Voto> votoes = new ArrayList<Voto>();
		
		con = DbClass.getConnection();
		Statement stm = con.createStatement();
		
		ResultSet result;
		result = stm.executeQuery("SELECT * FROM voto");
		
		while (result.next()) {
			Voto voto = new Voto(result.getLong(1), result.getString(2), result.getInt(3), result.getLong(4));
            votoes.add(voto);
        }		
		return votoes;
	}


	public Voto findById(long id) throws SQLException, ClassNotFoundException {
		Voto voto = null;
		
		con = DbClass.getConnection();
		Statement stm = con.createStatement();
		
		ResultSet result;
		result = stm.executeQuery("SELECT * FROM voto WHERE id = '" + id + "'");
		
		while (result.next()) {
			voto = new Voto(result.getLong(1), result.getString(2), result.getInt(3), result.getLong(4));
        }		
		return voto;
	}
	//retorna media de nota de avaliacao dos visitantes
	public long avg(long id) throws SQLException, ClassNotFoundException {
		Voto voto = null;
		
		con = DbClass.getConnection();
		Statement stm = con.createStatement();
		
		ResultSet result;
		result = stm.executeQuery("SELECT avg(nota) as nota from voto where id_noticia = '" + id + "'");
		 
		while (result.next()) {
			return result.getLong("nota");
        } 
		return 0;		 	 
	}	
	
	public ResultSet maisVotadas() throws SQLException, ClassNotFoundException {
		Voto voto = null;
		
		con = DbClass.getConnection();
		Statement stm = con.createStatement();
		
		return stm.executeQuery("SELECT n.*,avg(v.nota) as nota from voto v, noticia n WHERE n.id = v.id_noticia GROUP BY id_noticia ORDER BY nota desc");	 	
	}

}